package com.zhny.aiot.gd.sfj.plugin.device;

import com.zhny.aiot.gd.sfj.plugin.model.*;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.utils.MessageUtils;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.ModemDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.*;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

public class FertilizerDevice extends ModemDevice<FertilizerStoreEntry> {
    private final Map<String, FerilizerParamer> CommandOperations;
    public FertilizerDevice(ModemEngine engine,
                            IDeviceFactory<ModemConfig> factory,
                            ICenterFacilities centerFacilities,
                            DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
        CommandOperations =  new ImmutableMap.Builder<String, FerilizerParamer>()
                .put("openPump", new FerilizerParamer((short) 5,24,65280))
                .put("closePump", new FerilizerParamer((short)5,24,0))
                .put("openChannelA", new FerilizerParamer((short)5,25,65280))
                .put("closeChannelA", new FerilizerParamer((short)5,25,0))
                .put("openChannelB", new FerilizerParamer((short)5,26,65280))
                .put("closeChannelB", new FerilizerParamer((short)5,26,0))
                .put("openChannelC", new FerilizerParamer((short)5,27,65280))
                .put("closeChannelC", new FerilizerParamer((short)5,27,0))
                .put("openChannelD", new FerilizerParamer((short)5,58,65280))
                .put("closeChannelD", new FerilizerParamer((short)5,58,0))
                .put("openBucketA", new FerilizerParamer((short)5,54,65280))
                .put("closeBucketA",new FerilizerParamer((short)5,54,0))
                .put("openBucketB",new FerilizerParamer((short)5,55,65280))
                .put("closeBucketB",new FerilizerParamer((short)5,55,0))
                .put("openBucketC",new FerilizerParamer((short)5,56,65280))
                .put("closeBucketC",new FerilizerParamer((short)5,56,0))
                .put("openBucketD",new FerilizerParamer((short)5,57,65280))
                .put("closeBucketD",new FerilizerParamer((short)5,57,0))
                .put("readPumpState", new FerilizerParamer((short)2,0,1))
                .put("readMain",new FerilizerParamer((short)3,54,6)).build();
    }


    @Override
    public void onDecoder(ModemResponseMessage modemResponseMessage) {
        logger.info("---start decoder fertilizer message---");
        FertilizerResultModel resultModel = null;
        int function = modemResponseMessage.getFunction();
        switch (function){
            case 5:
                resultModel = new FertilizerCoilResultModel (modemResponseMessage,this);
                break;
            case 3:
                resultModel = new FerilizerTableResultModel (modemResponseMessage,this);
                break;
            case 2:
                resultModel= new FerilizerDiscreteResultModel (modemResponseMessage,this);
                break;
            default:
                MessageEntry messageEntry = MessageUtils.createMessageEntry (getId ());
                messageEntry.setType (MessageType.ERROR);
                messageEntry.setData ("response no support !");
                this.nofity (messageEntry);
                break;
        }
        resultModel.onHandler ();
    }

    @Override
    public ModemRequestMessage getCommand(TaskEntity taskEntity){
        MessageEntry entry= MessageUtils.createMessageEntry(this.getId ());
        entry.getProperties().put("task_name", taskEntity.getMethodName ());
        entry.setType (MessageType.OPERATION);
        FerilizerParamer command = CommandOperations.get (taskEntity.getMethodName ());
        if(command == null){
            entry.setType (MessageType.ERROR);
            entry.setData (String.format("factory[%s] no support command [%s]",getTypeId (),taskEntity.getMethodName ()));
            this.nofity (entry);
            return null;
        }

        return new ModemRequestMessage (
                Integer.valueOf (this.getConfig ().getAddress ()),
                command.getCode (),
                command.getRegisterAddress (),
                command.getData ()
                );
    }

    @Override
    protected void disposable() {

    }
}
